/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package p2p;

import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrador
 */
public class ServiciosArchivos {
    
    public String unirArchivo(List<Paquete> paquetes)throws Exception{
        Comparator<Paquete> c=new Comparator<Paquete>() {

            @Override
            public int compare(Paquete o1, Paquete o2) {
                return o1.parte-o2.parte;
            }
        };
        //probando comit
        Collections.sort(paquetes,c);
        File f=new File(paquetes.get(0).nombreAr);
        FileOutputStream file=new FileOutputStream(f);
        for(Paquete p:paquetes){
           file.write(p.data);
           file.flush();
        }
        file.close();
        

                            try {
                                
                            
                            BufferedReader in = new BufferedReader(new FileReader( f ) );
                            String leer = in.readLine();
                            String lectura = "";
                            while ( leer != null )
                            {
                            lectura+=("\n" + leer );
                            leer = in.readLine();
                            System.out.println(leer);
                            }
                            in.close();
                            
                           return  lectura;
                            
                            } catch (Exception ex) {
                                Logger.getLogger(Modelo.class.getName()).log(Level.SEVERE, null, ex);
                            }
        
        return "";
    }
    
    
    
}
